package com.osdp.uc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.osdp.uc.entity.UcPosition;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UcPositionMapper extends BaseMapper<UcPosition> {

    @Select("select * from uc_position up left join sc_role_position uup on up.id = uup.org_id where uup.user_id = #{userId}")
    List<UcPosition> queryPositionByUserId(String userId);

    /**
     * 用于岗位管理的分页主界面
     * @param position
     * @param page
     * @return
     */
    @Select({"<script>",
            "select up.* from uc_position up  " +
                    "where 1=1 " +
                    "<if test=\"position.orgId !=null and position.orgId !=''\">" +
                    "and up.org_id= #{position.orgId} " +
                    "</if>" +
                    "<if test=\"position.positionName !=null and position.positionName !=''\">" +
                    " and up.position_name like concat('%',#{position.positionName},'%') " +
                    "</if>" +
                    "<if test=\"position.positionCode !=null and position.positionCode !=''\">" +
                    " and up.position_code like concat('%',#{position.positionCode},'%') " +
                    "</if>" +
                    "order by up.create_time desc",
            "</script>"})
    IPage<UcPosition> pageListByOrgId(@Param("position") UcPosition position, Page<UcPosition> page);


    /**
     * 用于岗位选择的分页主界面，显示部门名称，职务名称
     * @param position
     * @param page
     * @return
     */
    @Select({"<script>",
            "select distinct up.*, uo.org_name " +
                    "from uc_position up left join uc_organization uo on up.org_id = uo.id " +
                    "where 1=1 " +
                    "<if test=\"position.orgId !=null and position.orgId !=''\">" +
                    "and up.org_id= #{position.orgId} " +
                    "</if>" +
                    "<if test=\"position.positionName !=null and position.positionName !=''\">" +
                    " and up.position_name like concat('%',#{position.positionName},'%') " +
                    "</if>" +
                    "<if test=\"position.positionCode !=null and position.positionCode !=''\">" +
                    " and up.position_code like concat('%',#{position.positionCode},'%') " +
                    "</if>" +
                    "order by up.create_time desc",
            "</script>"})
    IPage<UcPosition> selectPositionPageList(@Param("position") UcPosition position, Page<UcPosition> page);

    /**
     * 根据ID批量查询
     * @param ids
     * @return
     */
    @Select({"<script>",
            "select * from uc_position up where up.validation = '1' " +
            " <if test=\"ids!=null and ids.size >0\">" +
            "  and up.id in " +
            "  <foreach collection=\"ids\" separator=\",\" item=\"item\" open=\"(\" close=\")\">" +
            "   #{item} "+
            "  </foreach>" +
            "</if>",
            "</script>"}
    )
    List<UcPosition> queryPositionByIds(@Param("ids") List<String> ids);
}
